package com.feige.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feige.constant.Constants;
import com.feige.dao.GroupChatMsgMapper;
import com.feige.pojo.GroupChatMsg;
import com.feige.pojo.vo.ChatMsgVo;
import com.feige.service.GroupChatMsgService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 群聊消息表 服务实现类
 * </p>
 *
 * @author feige
 * @since 2021-05-10
 */
@Service
public class GroupChatMsgServiceImpl extends ServiceImpl<GroupChatMsgMapper, GroupChatMsg> implements GroupChatMsgService {


    @Override
    public int queryGroupMsgTotal(Integer pageNum, String groupId, Integer id) {
        HashMap<String, Object> map = new HashMap<>(10);
        map.put("groupId",groupId);
        map.put("id",id);
        return this.baseMapper.queryGroupMsgTotal(map);
    }

    @Override
    public List<ChatMsgVo> queryGroupMsg(Integer pageNum, String groupId, Integer id) {
        HashMap<String, Object> map = new HashMap<>(10);
        map.put("pageNum",(pageNum - 1) * Constants.CHAT_MSG_PAGE_SIZE);
        map.put("groupId",groupId);
        map.put("pageSize", Constants.CHAT_MSG_PAGE_SIZE);
        map.put("id",id);
        List<ChatMsgVo> chatMsgVos = this.baseMapper.queryGroupMsg(map);
        return chatMsgVos.stream().map(chatMsgVo -> {
            if (StrUtil.isNotBlank(chatMsgVo.getAvatar())){
                chatMsgVo.setAvatarType(1);
            }else {
                if (StrUtil.isBlank(chatMsgVo.getName())){
                    chatMsgVo.setName("课");
                }
                chatMsgVo.setAvatarType(3);
            }
            return chatMsgVo;
        }).collect(Collectors.toList());
    }
}
